package com.gzxw.mogublog.picture.controller;

import com.alibaba.fastjson.JSON;
import com.gzxw.mogublog.commons.entity.File;
import com.gzxw.mogublog.picture.service.FileService;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/uploadPicture")
    public R uploadPicture(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        String uploadFile = fileService.uploadPicture(file,request);
        return R.success("200", uploadFile);
    }


    @PostMapping("/getPicture")
    public R getPicture(@RequestParam("fileId") String fileId) {
        if (StringUtils.isEmpty(fileId)) {
            return R.error("200","无标题图");
        }

        File picture = fileService.getPicture(fileId);
        String pictureJson = JSON.toJSONString(picture);

        return R.success("200",pictureJson);
    }


    @PostMapping("/getPictures")
    public R getPictures(@RequestBody List<String> fileIds) {

        List<File> fileList = fileService.getPictures(fileIds);

        String data = JSON.toJSONString(fileList);
        return R.success("返回成功",data);
    }
}
